﻿using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Brushes;
using Microsoft.Graphics.Canvas.Effects;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using 纸片人.AttributeUsageFloder;
using 纸片人.InfoDataFolder;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace 纸片人.GpuPage
{
    [PageType("gpu","定位模糊")]
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class DirectionlBlurPage : Page
    {

        Vector2 pos;
        float radius = 30;
        float fade = 0.3f;
        float amount = 0.1f;
        //是否进入来了
        bool isEntry;
        CanvasRenderTarget renderTarget;

        float effectWidth = 0;
        float effectHeight = 0;
        float previewWidth = 0;
        float previewHeight = 0;

        float offsetX, offsetY, scale, width, height;

        //是否按下
        bool isPressed;

        public DirectionlBlurPage()
        {
            this.InitializeComponent();
            Init();
        }
        LayerBitmapData bitmapData = new LayerBitmapData();
   

     
        private void Init()
        {
            selectPicture.Click += async (s, e) =>
            {
                await bitmapData.SelectOneBitmapAsync(canvas.Device);
                if (bitmapData.oneBitmap != null)
                {
                    renderTarget = new CanvasRenderTarget(canvas, bitmapData.oneBitmap.Size);
                    CalcuationPosition();
                }
            };

            canvas.CreateResources += (s, e) =>
            {
                
                e.TrackAsyncAction(bitmapData.LoadOneShadeCodeAsync(Shaders.ShaderType.DirectionalBlur).AsAsyncAction());
            };
            canvas.Draw += (s, e) =>
            {
                Function.DrawGridGraphics(e.DrawingSession);

                if (bitmapData.oneBitmap != null)
                {
                    var previewTran = Function.CalcutateImageCenteredTransform(previewWidth, previewHeight, bitmapData.oneWidth, bitmapData.oneHeight);
                    previewTran.Source = bitmapData.oneBitmap;
                    e.DrawingSession.DrawImage(previewTran, 0, effectHeight);
                    //是否进入范围
                    if (bitmapData.oneShaderCode != null&&isEntry && (pos.X >= offsetX && pos.X <= offsetX + width && pos.Y >= offsetY && pos.Y <= offsetY + height))
                    {
                        var center = (pos - new Vector2(offsetX, offsetY)) / scale;
                        PixelShaderEffect effect = new PixelShaderEffect(bitmapData.oneShaderCode)
                        {
                            Source1 = bitmapData.oneBitmap,
                            Properties =
                            {
                                ["radius"]= radius,
                               ["pos"] = center,
                               ["fade"]= fade,
                               ["amount"]= amount,
                            },
                        };
                        using(var ds = renderTarget.CreateDrawingSession())
                        {
                            ds.Clear(Colors.Transparent);
                            ds.DrawImage(effect);
                            
                        }

                        var colors = renderTarget.GetPixelColors();

                        
                        var effectTran = Function.CalcutateImageCenteredTransform(effectWidth, effectHeight, bitmapData.oneWidth, bitmapData.oneHeight);
                        effectTran.Source = effect;
                        e.DrawingSession.DrawImage(effectTran);
                        e.DrawingSession.DrawCircle(pos, radius * scale, Colors.Black, 4f);
                        e.DrawingSession.DrawCircle(pos, radius * scale, Colors.White, 2f);

                    }
                    else
                    {
                        var originalTran = Function.CalcutateImageCenteredTransform(effectWidth, effectHeight, bitmapData.oneWidth, bitmapData.oneHeight);
                        originalTran.Source = bitmapData.oneBitmap;
                        e.DrawingSession.DrawImage(originalTran);
                    }
                }
            };
            //进入
            canvas.PointerEntered += (s, e) =>
            {
                isEntry = true;
            };
            //离开
            canvas.PointerExited += (s, e) =>
            {
                isEntry = false;
            };
            //移动
            canvas.PointerMoved += (s, e) =>
            {
                 pos = e.GetCurrentPoint(canvas).Position.ToVector2();

              

                CalcuationPosition();
            };
            //按下
            canvas.PointerPressed += (s, e) =>
            {
                isPressed = true;
                canvas.Invalidate();
            };
            //松开
            canvas.PointerReleased += (s, e) =>
            {
                isPressed = false;
            };
            //改变半径
            radiusSlider.ValueChanged += (s, e) =>
            {
                radius = (float)radiusSlider.Value * Math.Max(bitmapData.oneWidth, bitmapData.oneHeight) / 2f;
            };
            //大小更改
            canvas.SizeChanged += (s, e) =>
            {
                CalcuationPosition();
            };

            fadeSlider.ValueChanged += (s, e) =>
           {
               fade = (float)fadeSlider.Value;
           };

            amountSlider.ValueChanged += (s, e) =>
            {
                amount = (float)amountSlider.Value;
            };
        }
 
        void CalcuationPosition()
        {
            if (bitmapData.oneBitmap == null)
                return;

            radius = (float)radiusSlider.Value * Math.Max(bitmapData.oneWidth, bitmapData.oneHeight) / 2f;

            var element = canvas;
            effectWidth = (float)element.ActualWidth;
            effectHeight = (float)element.ActualHeight * 0.7f;
            previewWidth = effectWidth * 0.5f;
            previewHeight = (float)element.ActualHeight * 0.3f;

            var mat = Function.CalcutateImageCenteredMat(effectWidth, effectHeight, bitmapData.oneWidth, bitmapData.oneHeight);
            scale = mat.M11;
            offsetX = mat.M31;
            offsetY = mat.M32;
            width = scale * bitmapData.oneWidth;
            height = scale * bitmapData.oneHeight;
        }
    }
}
